<template>
    <!-- 最外层的大盒子 -->
    <div class="login_Bigbox">
        <!-- 登录盒子 -->
        <div class="login_box">
        <!-- 头像盒子 -->
        <div class="login_headportrait">
             <img alt="Vue logo" src="../assets/logo.png">
        </div>
        <!-- 登录表单 -->
        <el-form :model="loginForm" :rules="loginFormRules" ref="loginForm" class="login_Form">
        <el-form-item  prop="username">
        <el-input prefix-icon="el-icon-user-solid" v-model="loginForm.username"></el-input>
        </el-form-item>
         <el-form-item prop="password">
        <el-input prefix-icon="el-icon-lock" type="password" v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item class="login_btns">
        <el-button type="primary" @click="login">登录</el-button>
        <el-button type="info" @click="loginboxForm">重置</el-button>
        </el-form-item>
        </el-form>
        </div>
   
    </div>
</template>

<script>
import api from "../http/api";
// import { log } from 'util';
export default {
  data() {
    return {
      //登录表单数据
      loginForm: {
        username: "admin",
        password: "123456"
      },
      //登录表单数据验证规则
      loginFormRules:{
           username:[
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 5, max: 8, message: '长度在 5 到 8 个字符', trigger: 'blur' }
          ],
           password:[
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 5, max: 8, message: '长度在 5 到 8个字符', trigger: 'blur' }
          ],
      }
    };
  },
  methods: {
      // 重置表单方法
      loginboxForm(){
            this.$refs.loginForm.resetFields();
      },
      // 点击登录按钮
      login(){
          console.log(this);
          this.$refs.loginForm.validate(async(valid)=>{
              // 通过校验
              if(!valid) return
              // 发送请求到后端进行真正的登录
            //   const {data: res } = await axios.post("https://www.liulongbin.top:8888/api/private/v1/login", this.loginForm)
            // let {data:res}=await this.$axios.post("login",this.loginForm)
            // const {data: res } = await http.post('https://www.liulongbin.top:8888/api/private/v1/login', this.loginForm)
            let {data:res}=await api.login(this.loginForm)
              if(res.meta.status!==200) return this.$message.error(res.meta.msg)
              // 登录成功
              this.$message.success(res.meta.msg);
              // 把token值存储到本地存储
              window.sessionStorage.setItem("token",res.data.token)
              // 成功后--->跳转到首页
              await this.$router.push("/home")
          })
      }
  },
};
</script>

<style lang="scss" scoped>
.login_Bigbox{
    position: relative;
    background-color:#2b4b6b;
    height:100%;
    }
    .login_box{
        position: absolute;
        top: 50%;
        left: 50%;
        transform:translate(-50%, -50%);
        width: 450px;
        height: 300px;
        background-color: #ffffff;
        border-radius:3px;
        box-shadow: 0 0 15px #dddddd;
        }

        .login_headportrait{
            position: absolute;
            left: 50%;
            transform: translate(-50%,-50%);
            border: 1px solid #efefef;
            border-radius: 50%;
            padding: 10px;
            width: 150px;
            height: 150px;
            background-color: #ffffff;
            box-shadow: 0 0 15px #dddddd;
        }

        img{
            width: 100%;
            height: 100%;
            border-radius:50%;
            background-color: #efefef;
        }
    
        .login_Form{
            position: absolute;
            bottom: 0;
            padding: 0 20px;
            width: 100%;
        }
        
        .login_btns{
            display: flex;
            justify-content: flex-end;
        }


</style>